package com.siki.consumer.controller;

import com.siki.api.pojo.Dept;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptConsumerController {

    // 使用RestTemplate直接调用就可以了
    private RestTemplate restTemplate;
//    private final static String REST_STRING_8001="http://localhost:8001/";
    private final static String REST_STRING_8001="http://SPRINGCLOUD-PROVIDER-DEPT/";

    public DeptConsumerController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
    // 模式是(url,实体,class<T> responseType>
    @RequestMapping(value = "consumer/getAll")
    public List<Dept> getAllDept(){
        return restTemplate.getForObject(REST_STRING_8001+"dept/getAll",List.class);
    }

    @RequestMapping(value = "consumer/getDept/{id}")
    public Dept getOne(@PathVariable("id") Long id){
        // map是用来传递请求体的,get方法直接跟id就好
        return restTemplate.getForObject(REST_STRING_8001+"dept/getDept/"+id,Dept.class);
    }

    @RequestMapping(value = "consumer/addDept")
    public boolean addDept(@RequestParam Dept dept){
        return restTemplate.postForObject(REST_STRING_8001+"dept/addDept",dept,Boolean.class);
    }
}
